package com.ade.warehouse.service;

import java.util.*;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;

import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import util.IdWorker;

import com.ade.warehouse.dao.MenuDao;
import com.ade.warehouse.pojo.Menu;

/**
 * 服务层
 * 
 * @author Administrator
 *
 */
@Service
public class MenuService {

	@Autowired
	private MenuDao menuDao;
	
	@Autowired
	private IdWorker idWorker;


	
	public List<Menu> getMenuByParentId(String parentId) {
		HashMap whereMap=new HashMap();
		whereMap.put("parentId",parentId);
		Specification<Menu> specification = createSpecification(whereMap);
		Sort sort=new Sort(Sort.Direction.ASC,"sort");
		return menuDao.findAll(specification,sort);
	}

	
	public List<Menu> getMenuList() {
		Sort sort=new Sort(Sort.Direction.ASC,"sort");
		return menuDao.findAll(sort);
	}


	/**
	 * 查询菜单数据信息，生成树形菜单
	 * @return Result
	 */
	
	public List<Menu>  getMenu() {
		//查询所有列表数据
		List<Menu> MenuList = this.getMenuList();

		//查询顶级菜单
		List<Menu> rootMenuList = this.getMenuByParentId("0");

		//生成树形菜单数据，为一级菜单设置子菜单，getChild是递归调用的
		for (Menu menu : rootMenuList) {
			menu.setChildren(getChild(menu.getId(), MenuList));
		}

		return rootMenuList;
	}

	public List<Menu> getChild(String id, List<Menu> MenuList) {
		// 子菜单
		List<Menu> childList = new ArrayList<>();

		// 遍历所有节点，将父菜单id与传过来的id比较
		for (Menu menu : MenuList) {
			if (menu.getParentId() != null) {   //所有非以及菜单的都要加入chidList中（递归）
				if (menu.getParentId().equals(id)) {
					childList.add(menu);
				}
			}
		}
		// 递归退出条件
		if (childList.size() == 0) {
			return null;
		}
		// 把子菜单的子菜单再循环一遍
		for (Menu menu : childList) {// 没有url子菜单还有子菜单（暂不考虑url的问题）
			// 递归
			menu.setChildren(getChild(menu.getId(), MenuList));
		}
		return childList;
	}




	/**
	 * 查询全部列表
	 * @return
	 */
	public List<Menu> findAll() {
		return menuDao.findAll();
	}

	
	/**
	 * 条件查询+分页
	 * @param whereMap
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<Menu> findSearch(Map whereMap, int page, int size) {
		Specification<Menu> specification = createSpecification(whereMap);
		PageRequest pageRequest =  PageRequest.of(page-1, size);
		return menuDao.findAll(specification, pageRequest);
	}

	
	/**
	 * 条件查询
	 * @param whereMap
	 * @return
	 */
	public List<Menu> findSearch(Map whereMap) {
		Specification<Menu> specification = createSpecification(whereMap);
		return menuDao.findAll(specification);
	}

	/**
	 * 根据ID查询实体
	 * @param id
	 * @return
	 */
	public Menu findById(String id) {
		return menuDao.findById(id).get();
	}

	/**
	 * 增加
	 * @param menu
	 */
	public void add(Menu menu) {
		menu.setId( idWorker.nextId()+"" );
		menuDao.save(menu);
	}

	/**
	 * 修改
	 * @param menu
	 */
	public void update(Menu menu) {
		menuDao.save(menu);
	}

	/**
	 * 删除
	 * @param id
	 */
	public void deleteById(String id) {
		menuDao.deleteById(id);
	}

	/**
	 * 动态条件构建
	 * @param searchMap
	 * @return
	 */
	private Specification<Menu> createSpecification(Map searchMap) {

		return new Specification<Menu>() {

			
			public Predicate toPredicate(Root<Menu> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
                // 
                if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) {
                	predicateList.add(cb.like(root.get("id").as(String.class), "%"+(String)searchMap.get("id")+"%"));
                }
                // 
                if (searchMap.get("parentId")!=null && !"".equals(searchMap.get("parentId"))) {
                	predicateList.add(cb.like(root.get("parentId").as(String.class), "%"+(String)searchMap.get("parentId")+"%"));
                }
                // 
                if (searchMap.get("name")!=null && !"".equals(searchMap.get("name"))) {
                	predicateList.add(cb.like(root.get("name").as(String.class), "%"+(String)searchMap.get("name")+"%"));
                }
                // 
                if (searchMap.get("href")!=null && !"".equals(searchMap.get("href"))) {
                	predicateList.add(cb.like(root.get("href").as(String.class), "%"+(String)searchMap.get("href")+"%"));
                }
                // 
                if (searchMap.get("target")!=null && !"".equals(searchMap.get("target"))) {
                	predicateList.add(cb.like(root.get("target").as(String.class), "%"+(String)searchMap.get("target")+"%"));
                }
                // 
                if (searchMap.get("icon")!=null && !"".equals(searchMap.get("icon"))) {
                	predicateList.add(cb.like(root.get("icon").as(String.class), "%"+(String)searchMap.get("icon")+"%"));
                }
                // 
                if (searchMap.get("remarks")!=null && !"".equals(searchMap.get("remarks"))) {
                	predicateList.add(cb.like(root.get("remarks").as(String.class), "%"+(String)searchMap.get("remarks")+"%"));
                }
				
				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));

			}
		};

	}

}
